package com.hujf.hbase.config;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Properties;

/**
 * @author Hujf
 * @title: HdbConfig
 * @date 2021-07-20 15:18
 * @description: hbase 配置类
 */
public class HdbConfig {


    public static Configuration configuration;
    public static Connection connection;
    public static Admin admin;
    // 初始化链接
    public static void init() {
        configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "hadoop003");
        configuration.set("hbase.zookeeper.property.clientPort", "2181");
        configuration.set("zookeeper.znode.parent", "/hbase");

        try {
            connection = ConnectionFactory.createConnection(configuration);
            admin = connection.getAdmin();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Admin getHdbAdmin(){
        if(connection!=null){
            return admin;
        }else {
            HdbConfig.init();
        }
        return admin;
    }

    public static Connection getHdbConnection() {
        if(connection!=null){
            return connection;
        }else {
            HdbConfig.init();
        }
        return connection;
    }
}
